class_name Camera
extends Camera2D


const DISTANCE_TARGET := 100
const DURACTION_SHAKE := 120.0
const SMOOTHING_BALL_CARRIED := 2.0
const SMOOTHING_BALL_DEFAULT := 8.0
const SHKAE_INTENSITY := 5.0
var is_shake := false
var time_start_shake:= Time.get_ticks_msec()


@export var ball :Ball

func _init() -> void:
	GameEvents.impact_received.connect(on_impact_received.bind())

func _process(delta: float) -> void:
	if ball.carrier != null :
		position = ball.carrier.position + ball.carrier.heading * DISTANCE_TARGET
		position_smoothing_speed = SMOOTHING_BALL_CARRIED
	else:
		position = ball.position
		position_smoothing_speed = SMOOTHING_BALL_DEFAULT
		
	if is_shake and Time.get_ticks_msec() - time_start_shake < DURACTION_SHAKE:
		offset = Vector2(randf_range(-SHKAE_INTENSITY , SHKAE_INTENSITY),randf_range(-SHKAE_INTENSITY , SHKAE_INTENSITY))
	else:
		is_shake = false
		offset = Vector2.ZERO

func on_impact_received(_impact_position : Vector2, is_hight_impact:bool) -> void:
	if(is_hight_impact):
		is_shake = true
	
